import Vue from 'vue';

// 创建一个全局状态对象
const state = Vue.observable({
    lastUpdateTime: new Date(),
    refreshInterval: 3000, // 默认3秒刷新一次
    isRefreshing: false
});

// 创建一个全局的刷新管理器
const refreshManager = {
    // 开始全局刷新
    startGlobalRefresh() {
        if (state.isRefreshing) return;
        
        state.isRefreshing = true;
        state.refreshInterval = setInterval(() => {
            state.lastUpdateTime = new Date();
            // 触发全局刷新事件
            Vue.prototype.$bus.$emit('global-refresh');
        }, state.refreshInterval);
    },

    // 停止全局刷新
    stopGlobalRefresh() {
        if (!state.isRefreshing) return;
        
        clearInterval(state.refreshInterval);
        state.isRefreshing = false;
    },

    // 设置刷新间隔
    setRefreshInterval(interval) {
        state.refreshInterval = interval;
        if (state.isRefreshing) {
            this.stopGlobalRefresh();
            this.startGlobalRefresh();
        }
    },

    // 获取最后更新时间
    getLastUpdateTime() {
        return state.lastUpdateTime;
    }
};

export default refreshManager; 